﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;

namespace BS_2
{
    public partial class Form_UpdatePrj : CCWin.Skin_DevExpress
    {
        private string prj_code;
        public Form_UpdatePrj()
        {
            this.ShowInTaskbar = false;
            InitializeComponent();
        }
        private void showPrjinfo(string name)
        {
            string sql = "select * from project where prjname='" + name + "'";
            DataTable dt = DataAccess.Get_DataTable(sql, "prj");
            prj_code = dt.Rows[0]["prjcode"].ToString();
            prjname.Text = dt.Rows[0]["prjname"].ToString();
            prjcreator.Text = dt.Rows[0]["prjcreator"].ToString();
            prjdate.Text = Convert.ToDateTime(dt.Rows[0]["prjdate"].ToString()).ToString("yyyy-MM-dd");
            prjsource.Text = dt.Rows[0]["prjsource"].ToString();

            sql = "select dot_id,x,y from project_domain where prjcode=" + dt.Rows[0]["prjcode"].ToString() + " order by dot_id";
            DataTable dtdomain = DataAccess.Get_DataTable(sql, "prjdomain");
            dataGridView1.DataSource = dtdomain;
        }

        private void frmPrjOpen_Load(object sender, EventArgs e)
        {
            loaddata();
        }

        private void loaddata()
        {
            prjList.Items.Clear();
            string sql = "select prjname from project order by prjname";
            DataTable dt = DataAccess.Get_DataTable(sql, "prj");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    CCWin.SkinControl.SkinListBoxItem item = new CCWin.SkinControl.SkinListBoxItem(dt.Rows[i][0].ToString());
                    prjList.Items.Add(item);
                }
                prjList.SelectedIndex = 0;
                showPrjinfo(prjList.Items[0].ToString());
            }
        }

        private void prjList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void submit_Click(object sender, EventArgs e)
        {
            if (prjList.Items.Count > 0)
            {
                DialogResult dr = MessageBoxEx.Show("确认更新该工程信息吗？", "提示", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.OK)
                {
                    string sql = "select prjname from project where prjname='" + prjname.Text.Trim() + "' and prjcode<>" + prj_code;
                    string resultquery = DataAccess.Get_Row1_Col1_Value(sql);
                    if (resultquery != "")
                    {
                        MessageBoxEx.Show("工程名称已存在，请换名！", "输入提示");
                        return;
                    }
                    sql = "update project set prjname='" + prjname.Text.Trim() + "',prjcreator='" + prjcreator.Text.Trim() + "',prjsource='" + prjsource.Text.Trim() + "' where prjcode=" + prj_code;
                    int result = DataAccess.Run_SQL(sql);
                    if (result == 1)
                    {
                        int rows = Convert.ToInt16(dataGridView1.Rows.Count.ToString()) - 1;
                        if (rows < 3)
                        {
                            MessageBoxEx.Show("至少输入3个拐点坐标！", "输入提示");
                            return;
                        }
                        sql = "delete from project_domain where prjcode=" + prj_code;
                        DataAccess.Run_SQL(sql);

                        for (int i = 0; i < rows; i++)
                        {
                            int j = i + 1;
                            if (dataGridView1.Rows[i].Cells[0].Value.ToString() != "" && dataGridView1.Rows[i].Cells[1].Value.ToString() != "")
                            {
                                string sql_domain = "(" + prj_code + "," + j + "," + dataGridView1.Rows[i].Cells[0].Value + "," + dataGridView1.Rows[i].Cells[1].Value + ")";
                                sql = "insert into project_domain values" + sql_domain;
                                int resultdomain = DataAccess.Run_SQL(sql);
                                if (resultdomain == 0)
                                {
                                    MessageBoxEx.Show("更新工程坐标失败！", "提示");
                                    return;
                                }
                            }
                        }

                        MessageBoxEx.Show("更新成功！", "提示");
                        loaddata();
                    }
                    else
                    {
                        MessageBoxEx.Show("更新失败！", "提示");
                    }
                }
            }
        }

        private void prjList_Click(object sender, EventArgs e)
        {
            if (prjList.Items.Count > 0)
            {
                string name = prjList.SelectedItem.ToString();
                showPrjinfo(name);
            }
        }

        private void Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SkinButton1_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBoxEx.Show("确认删除该工程吗？", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                string name = prjList.SelectedItem.ToString();

                string sql = "select prjcode from project where prjname='" + name + "'";
                string prjcode = DataAccess.Get_Row1_Col1_Value(sql);


                sql = "delete from project where prjcode=" + prjcode;
                DataAccess.Run_SQL(sql);
                sql = "delete from project_domain where prjcode=" + prjcode;
                DataAccess.Run_SQL(sql);
                sql = "delete from subgeo where prjcode=" + prjcode;
                DataAccess.Run_SQL(sql);
                sql = "delete from subgeo_domain where prjcode=" + prjcode;
                DataAccess.Run_SQL(sql);
                DialogResult dr1 = MessageBoxEx.Show("该工程已删除！", "提示", MessageBoxButtons.OK);
                if (dr1 == DialogResult.OK)
                {
                    loaddata();
                }

            }
        }

        private void DataGridView1_RowPostPaint_1(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);

        }

        private void DataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (Regex.IsMatch(e.FormattedValue.ToString(), @"^[+-]?\d*[.]?\d*$") == false)
            {
                e.Cancel = true;
                MessageBoxEx.Show("拐点坐标必须是数值型");
                return;
            }
        }
    }
}
